import { deepCopy, rotate } from "@/utils/utils"

const tetrisBlocks = [
    [
        [0, 1, 0, 0],
        [0, 1, 0, 0],
        [0, 1, 0, 0],
        [0, 1, 0, 0],
    ], // 一
    [
        [0, 0, 0, 0],
        [0, 2, 2, 0],
        [0, 2, 2, 0],
        [0, 0, 0, 0],
    ], // 田
    [
        [0, 0, 0, 0],
        [3, 3, 3, 0],
        [0, 3, 0, 0],
        [0, 0, 0, 0],
    ], // T
    [
        [0, 0, 0, 0],
        [4, 4, 0, 0],
        [0, 4, 4, 0],
        [0, 0, 0, 0],
    ], // Z
    [
        [0, 0, 0, 0],
        [0, 5, 5, 0],
        [5, 5, 0, 0],
        [0, 0, 0, 0],
    ],  // S
    [
        [0, 6, 0, 0],
        [0, 6, 0, 0],
        [0, 6, 6, 0],
        [0, 0, 0, 0],
    ], // L
    [
        [0, 0, 7, 0],
        [0, 0, 7, 0],
        [0, 7, 7, 0],
        [0, 0, 0, 0],
    ],  // J
    [
        [7, 7, 7, 7],
        [7, 7, 7, 7],
        [7, 7, 7, 0],
        [7, 7, 7, 7],
    ],  // J
]
export { tetrisBlocks }

export default class Tetris {
    position = { col: 3, row: -3 }
    blocks: number[][]
    constructor(type: number, rotates = 0) {
        this.blocks = deepCopy(tetrisBlocks[type])
        if (rotates > 0) {
            for (let i = 0; i < rotates; i++) {
                this.blocks = rotate(this.blocks)
            }
        }
    }
    // 旋转
    rotate() {
        this.blocks = rotate(this.blocks)
    }

    // 移动
    move(col: number, row: number) {
        this.position.row += row
        this.position.col += col
    }
}